﻿using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Model;
using FubiManger.Controls;
using Newtonsoft.Json;
using System.Text.RegularExpressions;

namespace FubiManger.UI
{
    partial class AddgoodForm : Smobiler.Core.Controls.MobileForm
    {
        public AddgoodForm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }
        /// <summary>
        /// 商户ID
        /// </summary>
        public string merchantid { get; set; }
        /// <summary>
        /// 1上架2下架
        /// </summary>
        private string status = "1";
        /// <summary>
        /// 分类ID
        /// </summary>
        private string clasifyid = "";
        ms_goods good = new ms_goods();
        List<specs> splist = new List<specs>();
        /// <summary>
        /// 1新增2更新
        /// </summary>

        public string stype { get; set; }
        /// <summary>
        /// 商品ID
        /// </summary>
        public string goodsid { get; set; }

        /// <summary>
        /// 销售方式1到店2物流
        /// </summary>
        private string usage = "1";
        private void panclose_Press(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddgoodForm_Load(object sender, EventArgs e)
        {
            //merchantid = "2";
            ms_merchant merchant = merchantDal.Querybyid(merchantid);

            if (stype == "2")
            {
                good = goodsDal.Querybyid(goodsid);//查商品详情
                txtdescribes.Text = good.describes;//描述
                txtgoodname.Text = good.goods_name;//商品名称
                txtmarkprice.Text = good.market_price;//市场价

                txtprice.Text = good.price;//售价
                lblclassname.Text = classifyDal.Querybyid(good.classify_id);//分类名称
                txtspecs.Text = good.specs;
                //if (!string.IsNullOrEmpty(good.specs))
                //{
                //    splist = JsonConvert.DeserializeObject<List<specs>>(good.specs);

                //    if (splist.Count > 0)
                //    {
                //        lblspecs.Text = splist[0].key + ":" + splist[0].value + "....";
                //    }
                //}
                clasifyid = good.classify_id;//分类ID

                if (good.status == "1")
                {
                    status = "1";
                    swstatus.Checked = true;
                }
                else
                {
                    status = "2";
                    swstatus.Checked = false;
                }
                Array arr = good.carousel.Split(',');
                imgcontrol con = new imgcontrol();
                con.cover = @"C:\fbimage\" + good.cover;
                con.index = 0;
                con.stype = "1";
                panimg.Controls.Add(con);
                imglist.Add(good.cover);
                for (int i = 0; i < arr.Length; i++)
                {
                    imglist.Add(arr.GetValue(i).ToString());
                    int index = imglist.Count - 1;
                    imgcontrol con1 = new imgcontrol();
                    con1.cover = @"C:\fbimage\" + arr.GetValue(i).ToString();
                    con1.index = index;
                    con1.stype = "1";
                    panimg.Controls.Add(con1);
                }


            }

        }
        /// <summary>
        /// 菜单分类
        /// </summary>
        private void Getpop()
        {
            if (popgoodlist.Groups.Count > 0)
                popgoodlist.Groups.Clear();
            PopListGroup gp = new PopListGroup();
            List<ms_classify> cllist = classifyDal.Querybymerid(merchantid);
            for (int i = 0; i < cllist.Count; i++)
            {
                PopListItem item = new PopListItem();
                item.Text = cllist[i].title;
                item.Value = cllist[i].id;
                gp.Items.Add(item);
            }
            popgoodlist.Groups.Add(gp);
        }
        int index = 0;
        List<string> imglist = new List<string>();
        string cover = "";
        private void camera1_ImageCaptured(object sender, BinaryResultArgs e)
        {
            if (e.isError == false)
            {
                string name = DateTime.Now.ToString("yyyyMMddHHmmssms") + Getcode() + ".png";
                e.SaveFile(name, @"C:\fbimage\");
                imglist.Add(name);
                imgcontrol con = new imgcontrol();
                con.cover = @"C:\fbimage\" + name;
                con.index = imglist.Count - 1;
                con.stype = "1";
                panimg.Controls.Add(con);
            }

        }
        /// <summary>
        /// 随机4位数
        /// </summary>
        /// <returns></returns>
        private int Getcode()
        {
            Random rand = new Random();
            return rand.Next(1000, 9999);
        }
        /// <summary>
        /// 更新图片
        /// </summary>
        /// <param name="index"></param>
        /// <param name="imgname"></param>
        public void Updateimg(int index, string imgname)
        {
            imglist[index] = imgname;//更新图片
            panimg.Controls.Clear();
            for (int i = 0; i < imglist.Count; i++)
            {
                imgcontrol con1 = new imgcontrol();
                con1.cover = @"C:\fbimage\" + imglist[i];
                con1.index = i;
                con1.stype = "1";
                panimg.Controls.Add(con1);
            }
        }
        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="index"></param>
        public void Delimg(int index)
        {
            
            imglist.RemoveAt(index);
            panimg.Controls.Clear();
            for (int i = 0; i < imglist.Count; i++)
            {
                imgcontrol con1 = new imgcontrol();
                con1.cover = @"C:\fbimage\" + imglist[i];
                con1.index = i;
                con1.stype = "1";
                panimg.Controls.Add(con1);
            }
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panaddimg_Press(object sender, EventArgs e)
        {
            camera1.GetPhoto();
        }
        /// <summary>
        /// 上下架
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void swstatus_CheckedChanged(object sender, EventArgs e)
        {
            if (swstatus.Checked == true)
                status = "1";
            else
                status = "2";
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pansave_Press(object sender, EventArgs e)
        {
            if (imglist.Count == 0)
            {
                MessageBox.Show("请上传封面");
            }
            else if (imglist.Count < 2)
            {
                MessageBox.Show("请上传详情图");
            }
            else if (string.IsNullOrEmpty(txtgoodname.Text))
            {
                MessageBox.Show("请填写商品名称");
            }
            else if (string.IsNullOrEmpty(txtdescribes.Text))
            {
                MessageBox.Show("请填写商品描述");
            }

            else if (string.IsNullOrEmpty(lblclassname.Text))
            {
                MessageBox.Show("请选择商品分类");
            }
            else if (Convert.ToDouble(txtmarkprice.Text) < 1)
            {
                MessageBox.Show("请填写商品市场价");
            }
            else if (Convert.ToDouble(txtprice.Text) < 1)
            {
                MessageBox.Show("请填写商品售价");
            }
            else
            {


                for (int i = 1; i < imglist.Count; i++)
                {

                    if (i == 1)
                        good.carousel = imglist[i];
                    else
                        good.carousel += "," + imglist[i];
                }
                try
                {

                    good.cover = imglist[0];//封面                    
                    good.goods_name = txtgoodname.Text;//商品名称
                    good.describes = txtdescribes.Text;//描述
                    good.price = txtprice.Text;//售价
                    good.market_price = txtmarkprice.Text;//市场价
                    good.classify_id = clasifyid; //分类ID
                    good.merchant_id = merchantid;//商家ID
                    good.status = status;//是否上架  
                    good.specs = txtspecs.Text;
                    if (stype == "2")
                    {
                        if (goodsDal.Updategoods(good, goodsid) > 0)
                        {
                            Toast("更新成功");
                            this.Close();
                        }
                    }
                    else
                    {
                        if (goodsDal.Querybyname(txtgoodname.Text, merchantid) < 1)
                        {
                            if (goodsDal.Addgoods(good) > 0)
                            {
                                MessageBox.Show("商品上传成功");
                                cover = "0";
                                panimg.Controls.Clear();
                                txtdescribes.Text = "";
                                txtgoodname.Text = "";
                                txtmarkprice.Text = "0";
                                txtprice.Text = "0";
                                txtspecs.Text = "";
                                clasifyid = "";
                                imglist.Clear();
                                //GoodsForm frm = new GoodsForm();
                                //frm.merchantid = merchantid;
                                //this.Form.Show(frm, true, (obj, args) =>
                                //{
                                //    this.Close();
                                //});
                            }
                        }
                        else
                            MessageBox.Show("该商品名称已存在,请勿重复添加");
                    }

                }
                catch (Exception ex)
                {

                    MessageBox.Show("操作异常请联系客服经理");
                    // MessageBox.Show(ex.ToString());
                }
            }
        }
        /// <summary>
        /// 选择分类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panclassify_Press(object sender, EventArgs e)
        {
            Getpop();//加载商品分类
            popgoodlist.ShowDialog();
        }

        private void popgoodlist_Selected(object sender, EventArgs e)
        {
            lblclassname.Text = popgoodlist.Selection.Text;
            clasifyid = popgoodlist.Selection.Value;
        }
        /// <summary>
        /// 规格设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panspecs_Press(object sender, EventArgs e)
        {
            //SpecsFrom frm = new SpecsFrom();
            //frm.splist = splist;
            //this.Form.Show(frm, (obj, args) =>
            //{
            //    splist = frm.splist;
            //    if (splist.Count > 0)
            //    {
            //        lblspecs.Text = splist[0].key + ":" + splist[0].value + "....";
            //    }
            //});
        }

        private void camera2_ImageCaptured(object sender, BinaryResultArgs e)
        {
            if (e.isError == false)
            {
                index++;
                string name = DateTime.Now.ToString("yyyyMMddHHmmssms") + Getcode() + ".png";
                e.SaveFile(name, @"C:\fbimage\");
                if (index > 1)
                {
                    imglist.Add(name);
                }
                else
                    cover = name;
                imgcontrol con = new imgcontrol();
                con.cover = @"C:\fbimage\" + name;
                con.index = index;
                panimg.Controls.Add(con);
            }
        }


        private void txtgoodname_TouchLeave(object sender, EventArgs e)
        {
            txtgoodname.Text = Regex.Replace(txtgoodname.Text, @"[^a-zA-Z0-9\u4e00-\u9fa5\s]", "");
        }

        private void txtdescribes_TouchLeave(object sender, EventArgs e)
        {
          //  txtdescribes.Text = Regex.Replace(txtdescribes.Text, @"[^a-zA-Z0-9\u4e00-\u9fa5\s]", "");
        }

        private void txtmarkprice_TouchEnter(object sender, EventArgs e)
        {
            txtmarkprice.Text = "";
        }

        private void txtprice_TouchEnter(object sender, EventArgs e)
        {
            txtprice.Text = "";
        }

        private void barcodeScanner1_BarcodeScanned(object sender, BarcodeResultArgs e)
        {
           
            if (e.Value != null)
            {
                ms_generic_goods good = generic_goodsDal.Querybycode(e.Value);
                if (!string.IsNullOrEmpty(good.gg_goods_name))
                {

                    txtdescribes.Text = good.gg_describes;//描述
                    txtgoodname.Text = good.gg_goods_name;//商品名称 
                    txtspecs.Text = good.gg_specs;
                    Array arr = good.gg_carousel.Split(',');
                    imgcontrol con = new imgcontrol();
                    con.cover = @"C:\fbimage\" + good.gg_cover;
                    con.index = 0;
                    con.stype = "1";
                    panimg.Controls.Add(con);
                    imglist.Add(good.gg_cover);
                    for (int i = 0; i < arr.Length; i++)
                    {
                        imglist.Add(arr.GetValue(i).ToString());
                        int index = imglist.Count - 1;
                        imgcontrol con1 = new imgcontrol();
                        con1.cover = @"C:\fbimage\" + arr.GetValue(i).ToString();
                        con1.index = index;
                        con1.stype = "1";
                        panimg.Controls.Add(con1);
                    }
                }
                else
                {
                    MessageBox.Show("该条码暂无商品");
                }
            }
        }
        /// <summary>
        /// 扫码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnscan_Press(object sender, EventArgs e)
        {
            //imglist.Clear();
            //index = 0;
            //barcodeScanner1.GetBarcode();
            //List<ms_generic_goods> gdlist = generic_goodsDal.Querylist();
            //for (int i = 0; i < gdlist.Count; i++)
            //{
            //    good = new ms_goods();
            //    good.cover = gdlist[i].gg_cover;//封面
            //    good.carousel = gdlist[i].gg_carousel;//轮播图
            //    good.goods_name = gdlist[i].gg_goods_name;//商品名称
            //    good.describes = gdlist[i].gg_describes;//描述
            //    good.price = "0";//售价
            //    good.market_price ="0";//市场价
            //    good.classify_id = "33"; //分类ID
            //    good.merchant_id = merchantid;//商家ID
            //    good.status = "1";//是否上架  
            //    good.specs = gdlist[i].gg_specs;
            //    good.weight = gdlist[i].gg_weight;//净重
            //    good.bar_code = gdlist[i].gg_bar_code;//条码
            //    good.brand = gdlist[i].gg_brand;//品牌
            //    good.guarantee = gdlist[i].gg_guarantee;//保质期
            //    goodsDal.Addgoods1(good);
            //}
        }
    }
}